package com.wifi.game.dao;

import com.wifi.game.entity.GameEntity;
import com.wifi.unti.DBHelper;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class GameDaoImp implements  GameDao{

//    public List<Object> findAll(int star,int pageSize) throws SQLException {
//        String sql="SELECT name,title,downloadCount,size,status,type,recType,platform        FROM tb_game limit ?,?";
//        List<Object> objects = DBHelper.selectAll(sql,new GameEntity(),star,pageSize);
//        System.out.println(objects);
//        return objects;
//    }
public List<Object> findAll(int star,int pageSize) throws SQLException {
    String sql="SELECT *       FROM tb_game limit ?,?";
    List<Object> objects = DBHelper.selectAll(sql,new GameEntity(),star,pageSize);
    return objects;
}


    @Override
    public void insert(GameEntity gameEntity) throws SQLException {
         String sql="INSERT INTO `shenzhou`.`tb_game`(`name`, `title`, `downloadCount`, `size`, `des`, `gameIcon`, `iosUrl`, `androidUrl`, `pictures`, `status`, `type`, `recType`, `platform`) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)";
        Long insert = DBHelper.insert(sql,  gameEntity.getName(), gameEntity.getTitle(), gameEntity.getDownloadCount(), gameEntity.getSize(),
                gameEntity.getDes(), gameEntity.getGameIcon(), gameEntity.getIosUrl(), gameEntity.getAndroidUrl(), gameEntity.getPictures(),gameEntity.getStatus(), gameEntity.getType(),gameEntity.getRecType(), gameEntity.getPlatform());;
    }

    @Override
    public long count() throws SQLException {
        QueryRunner runner= new QueryRunner(DBHelper.getDataSource());
         String  sql="SELECT COUNT(*) FROM `shenzhou`.`tb_game`";
        Long count = runner.query(sql, new ScalarHandler<Long>());
        System.out.println(count);
        return  count;



    }

    @Override    //通过gid查找
    public Object find(int gid) throws SQLException {
    String sql="SELECT * FROM `shenzhou`.`tb_game` WHERE gid=?";
        Object select = DBHelper.select(sql, new GameEntity(), gid);
        return select;
    }

    @Override
    public void edit(int gid, GameEntity gameEntity) throws SQLException {
        String sql="UPDATE `shenzhou`.`tb_game` SET `name` = ?, `title` = ?, `downloadCount` = ?, `size` = ?, `des` = ?, `gameIcon` = ?, `iosUrl` = ?, `androidUrl` = ?, `pictures` = ?, `status` = ?, `type` = ?, `recType` = ?, `platform` = ? WHERE `gid` = ?";
        int update = DBHelper.update(sql, gameEntity.getName(), gameEntity.getTitle(), gameEntity.getDownloadCount(), gameEntity.getSize(),
                gameEntity.getDes(), gameEntity.getGameIcon(), gameEntity.getIosUrl(), gameEntity.getAndroidUrl(), gameEntity.getPictures(), gameEntity.getStatus(), gameEntity.getType(), gameEntity.getRecType(), gameEntity.getPlatform(), gid);
        System.out.println(update);

}

    @Override
    public void remove(int gid) throws SQLException {
        String sql="UPDATE `shenzhou`.`tb_game` SET `status` = 2 WHERE `gid` = ?";
        int update = DBHelper.update(sql, gid);
        System.out.println(update);
    }


}
